package com.wang.lotterysystem.dao.mapper;

import com.wang.lotterysystem.dao.dataobject.Encrypt;
import com.wang.lotterysystem.dao.dataobject.user.UserDO;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

@Mapper
public interface UserMapper {
    @Select("select count(*) from user where email = #{mail}")
    int countByMail(@Param("mail") String mail);

    @Select("select count(*) from user where phone_number = #{phoneNumber}")
    int countByPhoneNumber(@Param("phoneNumber") Encrypt phoneNumber);

    @Insert("insert into user(user_name, email, phone_number, password, identity) " +
            "values (#{userName},#{email},#{phoneNumber},#{password},#{identity})")
    int insert(UserDO user);

    @Select("select " +
            "id, gmt_create, gmt_modified, user_name, email, phone_number, password, identity " +
            "from user where phone_number = #{phoneNumber}")
    UserDO selectByPhoneNumber(Encrypt phoneNumber);

    @Select("select " +
            "id, gmt_create, gmt_modified, user_name, email, phone_number, password, identity " +
            "from user where email = #{email}")
    UserDO selectByEmail(String email);

    @Select("<script>"
            + " SELECT id, gmt_create, gmt_modified, user_name, email, phone_number, password, identity FROM user"
            + " <if test=\"identity != null\">"
            + " WHERE identity = #{identity}"
            + " </if>"
            + " order by id desc"
            + " </script>")
    List<UserDO> selectByIdentity(String identity);

    @Select("<script>" +
            "select id from user where id in " +
            "<foreach collection = 'userId' item = 'id' open = '(' close = ')' separator = ','>" +
            "#{id}" +
            "</foreach>" +
            "</script>")
    List<Long> batchExistByIds(List<Long> userId);

    @Select("<script>" +
            "select * from user where id in " +
            "<foreach collection = 'userId' item = 'id' open = '(' close = ')' separator = ','>" +
            "#{id}" +
            "</foreach>" +
            "</script>")
    List<UserDO> selectByUserIdList(List<Long> userId);
}